package com.lovo.lesson8.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.lesson8.model.StudentDO;
import com.lovo.lesson8.dao.StudentDAO;
import com.lovo.lesson8.model.dto.StudentDTO;
import com.lovo.lesson8.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.lesson8.util.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bdk
 * @since 2020-11-24
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDAO, StudentDO> implements StudentService {
    @Resource
    private StudentDAO studentDAO;
    @Resource
    private RedisUtil redisUtil;
    @Override
    public Page selectAll(Integer page, Integer size) {
        Page pageObj = null;
        // 判断缓冲中是否存在查询的学生信息
        pageObj = (Page) redisUtil.get("selectAll="+page+"="+size);
        if (pageObj == null) {
            pageObj = new Page(page,size);
            List list = studentDAO.selectAll(pageObj);
            pageObj.setRecords(list);
            redisUtil.set("selectAll="+page+"="+size,pageObj);
        }

        return pageObj;
    }

    @Override
    public void saveStudent(StudentDTO studentDTO) {
        StudentDO studentDO = new StudentDO();
        BeanUtils.copyProperties(studentDTO,studentDO);
        studentDO.setStudentId(redisUtil.generateOrderId());
        studentDAO.insert(studentDO);
    }

    @Override
    public StudentDTO selectById(String studentId) {
        StudentDO studentDO =  studentDAO.selectById(studentId);
        return null;
    }
}
